package GUI;

import Client.Client;
import GUITOOl.MyButton;
import GUITOOl.MyLineBorder;
import Monitor.ClientThread;
import Service.LoginIn;
import Utils.GetWindowsSize;
import Utils.ImageIconUtils;
import Utils.ThreadManage;
import Utils.Validate;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import static java.lang.System.exit;

/**
 * The type Login frame.
 *
 * @author dzr, wyc, zry
 * @version 1.0
 */
public class LoginFrame extends JFrame {
    private JLabel uid_lable;
    private JLabel pwd_lable;
    private JTextField uid_text;
    private JPasswordField pwd_text;
    private JButton loginB;
    private JButton registerB;

    /**
     * Main entrance of Program.
     *
     * @param args the args
     */
    public static void main(String[] args){
        try {
            Client.start();
            new LoginFrame().init();
        } catch (SocketException e) {
            e.printStackTrace();
            System.out.println("程序错误，请重试！");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }

    /**
     * Init Login Frame.
     */
    public void init(){

        MyLineBorder myLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
        Color bgColor = new Color(255, 255, 255);
        setTitle("featherChat");
        setFont(new Font("MV Boil",0,14));
        setSize(480,350);
        setLocation(GetWindowsSize.getWindowsSize().width / 2 - this.getWidth() / 2, GetWindowsSize.getWindowsSize().height / 2 - this.getHeight() / 2);
        getContentPane().setBackground(bgColor);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        BorderLayout border_layout=new BorderLayout();
        setLayout(border_layout);

        JPanel panel_N=new JPanel();
        panel_N.setLayout(null);
        panel_N.setPreferredSize(new Dimension(0, 140));
        panel_N.setBackground(bgColor);
        ImageIcon image=ImageIconUtils.getRawPicture("title5.png");
        JLabel background=new JLabel(image);
        background.setBounds(0,-30,500,150);
        panel_N.add(background);
        add(panel_N,BorderLayout.NORTH);

        JPanel panel_C=new JPanel();
        panel_C.setLayout(null);
        panel_C.setPreferredSize(new Dimension(0, 180));
        panel_C.setBackground(bgColor);

        uid_lable = new JLabel("账号");
        pwd_lable = new JLabel("密码");
        uid_lable.setFont(new Font("微软雅黑",0,14));
        uid_lable.setLocation(0,15);
        uid_lable.setSize(30,30);
        pwd_lable.setFont(new Font("微软雅黑",0,14));
        pwd_lable.setLocation(0,50);
        pwd_lable.setSize(30,30);
        uid_text = new JTextField();
        uid_text.setBounds(50, 15, 175, 30);
        uid_text.setBorder(myLineBorder);
        pwd_text = new JPasswordField(JPasswordField.LEFT);
        pwd_text.setBounds(50, 50, 175, 30);
        pwd_text.setBorder(myLineBorder);
        JCheckBox rember_pwd=new JCheckBox("记住密码");
        rember_pwd.setBounds(0, 80, 80, 20);
        JCheckBox login_auto=new JCheckBox("自动登录");
        login_auto.setBounds(100, 80, 80, 20);
        panel_C.add(uid_lable);
        panel_C.add(uid_text);
        panel_C.add(pwd_lable);
        panel_C.add(pwd_text);
        add(panel_C,BorderLayout.CENTER);

        JPanel panel_west=CreatePanel.CreateWestPanel();
        panel_west.setBackground(bgColor);
        add(panel_west,BorderLayout.WEST);

        JPanel panel_S=new JPanel();
        panel_S.setLayout(null);
        panel_S.setPreferredSize(new Dimension(0, 50));
        panel_S.setBackground(bgColor);
        loginB = new MyButton("登录",1);
        loginB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginIn();
            }
        });
        registerB = new MyButton("注册",1);
        registerB.addActionListener(new registerAction());
        loginB.setBounds(150,0,50,30);
        loginB.setBorder(myLineBorder);
        panel_S.add(loginB);
        registerB.setBounds(265,0,50,30);
        registerB.setBorder(myLineBorder);
        panel_S.add(registerB);
        add(panel_S,BorderLayout.SOUTH);

        Image frameImage= ImageIconUtils.getRawPicture("picture.png").getImage();
        setIconImage(frameImage);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        pwd_text.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar() ==KeyEvent.VK_ENTER) {
                    loginIn();
                    LoginFrame.this.dispose();
                }
            }
        });
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                exit(0);
            }
        });
    }

    /**
     * Login in.
     */
    public void loginIn(){
        if (Validate.isEmpty(uid_text)){
            {
                new showMessageFrame("请输入账号");
            }

        }else if (Validate.isEmpty(pwd_text)){
            {
                new showMessageFrame("请输入密码");
            }
        }else {
            try {
                int uid = Integer.parseInt(uid_text.getText());
                String password = new String(pwd_text.getPassword());
                if (LoginIn.loginIn(uid, password, Client.client)){
                    ClientThread clientThread = ThreadManage.getInstance().getClientThreads().get(uid);
                    clientThread.setMainFrame(new MainFrame(uid, clientThread.getProfileID(), clientThread.getUsername(), clientThread.getSex(), clientThread.getBirthday()));
                    LoginFrame.this.dispose();
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

        }
    }

    /**
     * registerAction Listener
     * @author dzr
     * @version 1.0
     */
    private class registerAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            new RegisterFrame().init();
        }
    }

}
